<?php 
date_default_timezone_set("Australia/Melbourne");
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<style type="text/css">
/* <![CDATA[ */

table, td
{
    border-color: #666;
    border-style: solid;
}
table, th
{
    border-color: #666;
    border-style: solid;
}

table
{
    border-width: 0 0 1px 1px;
    border-spacing: 0;
    border-collapse: collapse;
}
 
td
{
    margin: 0;
    padding: 4px;
    border-width: 1px 1px 0 0;
}
th
{
    margin: 0;
    padding: 4px;
    border-width: 1px 1px 0 0;
}

/* ]]> */
</style>
</head>
<body style="font-family:Arial, Helvetica, sans-serif">
<h1>Shop Target Version 1.01 : <?php echo $this->dateBegin ?> to <?php echo $this->dateEnd ?></h1>
** Please note this is a "FOR YOUR REFERENCE ONLY" version<br />
<br />

<?php
$arrWeek = $this->arrWeek;
$weeks = count($arrWeek);
$staffKPI = $this->staffKPI;
$stffInfo = new Model_DbTable_Roster_Stafflogindetail(Zend_Registry::get('db_remoteapp'));
$sh = new Model_DbTable_Shoplocation(Zend_Registry::get('db_remoteapp'));
?>
<span style="background-color:#C90; width:20px; display:inline-block">&nbsp;&nbsp;&nbsp;</span> Just Miss
<span style="background-color:#0F0; width:20px; display:inline-block">&nbsp;&nbsp;&nbsp;</span> Doing Great
<span style="background-color:#C30; width:20px; display:inline-block">&nbsp;&nbsp;&nbsp;</span> Way Way Below
<span style="background-color:#3CF; width:20px; display:inline-block">&nbsp;&nbsp;&nbsp;</span>
Reach Target<br />
<br />

 <table width="1550" border="0" cellspacing="1" cellpadding="5" style="font-size:13px;">
 
<?php 
$oldManager = 2;
$bSum = 0;
//$cot = 0;
foreach($this->arrRes as $k => $v) { ?>
	 <tr>
    <td rowspan="2">Manager</td>
    <td rowspan="2">Shop</td>
    <td rowspan="2">Monthly Target<br />    </td>
    <td rowspan="2">Monthly Target2<br />    </td>
    <td rowspan="2">Monthly Target3<br />    </td>
    <td colspan="3" align="center">
      <?php
    if(isset($arrWeek[0])){
		echo $arrWeek[0]["date_begin"]." - ".$arrWeek[0]["date_end"];
		}
	?>
    </td>
    <td colspan="3" align="center">    <?php
    if(isset($arrWeek[1])){
		echo $arrWeek[1]["date_begin"]." - ".$arrWeek[1]["date_end"];
		}
	?></td>
    <td colspan="3" align="center">    <?php
    if(isset($arrWeek[2])){
		echo $arrWeek[2]["date_begin"]." - ".$arrWeek[2]["date_end"];
		}
	?></td>
    <td colspan="3" align="center">    <?php
    if(isset($arrWeek[3])){
		echo $arrWeek[3]["date_begin"]." - ".$arrWeek[3]["date_end"];
		}
	?></td>
    <td colspan="3" align="center">    <?php
    if(isset($arrWeek[4])){
		echo $arrWeek[4]["date_begin"]." - ".$arrWeek[4]["date_end"];
		}
	?></td>
  
    <td>Monthly <br />
    Actual</td>
    <td>Bonus Achieve</td>
      <!--
    <td rowspan="2">Monthly <br />
    Target</td>
    <td rowspan="2">Total Bonus</td>
    -->
  </tr>
  <tr>
    <th colspan="2"  scope="col">Week1</th>
    <th scope="col">Bonus</th>
    <th colspan="2" scope="col">Week2</th>
    <th scope="col">Bonus</th>
    <th colspan="2" scope="col">Week3</th>
    <th scope="col">Bonus</th>
    <th colspan="2" scope="col">Week4</th>
    <th scope="col">Bonus</th>
    <th colspan="2" scope="col">Week5</th>
    <th scope="col">Bonus</th>
    <th scope="col">&nbsp;</th>
    <th scope="col">&nbsp;</th>
   </tr>

<?php 	

	$idManager = 0;
	$idManager = $v['staff_id'];
	
	//$cot++;
	$target = 0;
	$hitCot = 0;
	$target = $v['target_week'];
	$target2 = $v['target_2_week'];
	$target3 = $v['target_3_week'];
	$shopBudget = $sh->getShopLimits($v['shop']);
	$shopKPI = round($target/$shopBudget,2);
	?>  
    <tr>
    <td><?php
     	
	$mgName = $stffInfo->getDetail($idManager); 
	echo ucwords($mgName["ni"]);	
		?></td>
    <td><?php echo $v['shop'];?></td>
    <td> $ <?php echo $v['target'];?></td>
    <td> $ <?php echo $v['target_2'];?></td>
    <td> $ <?php echo $v['target_3'];?></td>
    <td><?php echo $v[0];?></td>
    <td><span
    <?php
    $dif = $v[0] - $target;
	$color = "";
	if($dif > 1000){
		$color = "background-color:#0F0;";
		}
	if($dif > -100 && $dif<=0){
		$color = "background-color:#C90;";
		}
	if($dif < -1000){
		$color = "background-color:#C30;";
		}		
	?>
    style="font-size:10px;<?php echo $color;?>" >[<?php echo round($dif,2); ?>]</span></td>
    <td
    <?php if($dif >0 ){
		$hitCot++;
		echo 'style="background-color:#3CF"';
		}?>
    ><?php //echo round($v[3],2); 
		if($dif >0 ){
		echo "Achieved";	
		}
		else{
		echo "No";	
		}
	
	?></td>
    <td><?php echo $v[1];?></td>
    <td><span
        <?php
    $dif = $v[1] - $target;
	$color = "";
	if($dif > 1000){
		$color = "background-color:#0F0;";
		}
	if($dif > -100 && $dif<=0){
		$color = "background-color:#C90;";
		}
	if($dif < -1000){
		$color = "background-color:#C30;";
		}		
	?>
    style="font-size:10px;<?php echo $color;?>" >[<?php echo round($dif,2); ?>]</span></td>
    <td
        <?php if($dif >0 ){
		$hitCot++;
		echo 'style="background-color:#3CF"';
		}?>
    
    ><?php //echo round($v[5],2);
			if($dif >0 ){
		echo "Achieved";	
		}
		else{
		echo "No";	
		}
	?></td>
    <td><?php echo $v[2];?></td>
    <td><span
        <?php
    $dif = $v[2] - $target;
	$color = "";
	if($dif > 1000){
		$color = "background-color:#0F0;";
		}
	if($dif > -100 && $dif<=0){
		$color = "background-color:#C90;";
		}
	if($dif < -1000){
		$color = "background-color:#C30;";
		}		
	?>
    style="font-size:10px;<?php echo $color;?>" >[<?php echo round($dif,2); ?>]</span></td>
    <td
        <?php if($dif >0 ){
		$hitCot++;
		echo 'style="background-color:#3CF"';
		}?>
    
    ><?php //echo round($v[7],2);
			if($dif >0 ){
		echo "Achieved";	
		}
		else{
		echo "No";	
		}
	?></td>
    <td><?php echo $v[3];?></td>
    <td><span
        <?php
    $dif = $v[3] - $target;
	$color = "";
	if($dif > 1000){
		$color = "background-color:#0F0;";
		}
	if($dif > -100 && $dif<=0){
		$color = "background-color:#C90;";
		}
	if($dif < -1000){
		$color = "background-color:#C30;";
		}		
	?>
    style="font-size:10px;<?php echo $color;?>" >[<?php echo round($dif,2); ?>]</span></td>
    <td
       <?php if($dif >0 ){
		$hitCot++;
		echo 'style="background-color:#3CF"';
		}?> 
    
    ><?php //echo round($v[9],2);
			if($dif >0 ){
		echo "Achieved";	
		}
		else{
		echo "No";	
		}
	?></td>
    <td>
    <?php 
		if($weeks == 4) {
			echo '-';
			}
		else{
			
			echo $v[4];
			
			}	

	?>
	</td>
	<td>
     <?php 
		if($weeks == 4) {
			}    
   		else{
		$dif = $v[4] - $target;
		$color = "";
	if($dif > 1000){
		$color = "background-color:#0F0;";
		}
	if($dif > -100 && $dif<=0){
		$color = "background-color:#C90;";
		}
	if($dif < -1000){
		$color = "background-color:#C30;";
		}	
		?>
	   <span  style="font-size:10px;<?php echo $color;?>" >[<?php echo round($dif,2); ?>]</span>  	
	<?php	
		}
		?>
    </td>
    <td>
	<?php //echo round($v[11],2)
		if($weeks == 4) {
			echo '-';
		}else{
	
			if($dif >0 ){
				echo "Achieved";	
			}else{
				echo "No";	
			}
		}
	?>
    </td>
<td>
    $ <?php
		echo $v['mon_sum']
	?>
    
    </td>    
 <td>
    <?php
  	$targetLevel = '';
	if(($v['mon_sum'] - $v['target']) >=0){
		$targetLevel = "Achieve Level 1";
		}
	if(($v['mon_sum'] - $v['target_2']) >=0){
		$targetLevel = "Achieve Level 2";
		}
	if(($v['mon_sum'] - $v['target_3']) >=0){
		$targetLevel = "Great Level 3";
		}		
	echo $targetLevel;
	
	?>
    
    </td>    
    
    
  </tr>
  <?php if(isset($staffKPI[$v['shop']])){ 
  			$w1avgKPI = 0;
  			$w2avgKPI = 0;
  			$w3avgKPI = 0;
  			$w4avgKPI = 0;
  			$w5avgKPI = 0;
  			$avgKPI = 0;
  			$w1count = 0;
  			$w2count = 0;
  			$w3count = 0;
  			$w4count = 0;
  			$w5count = 0;
  	?>
  
  <tr>
  	<th colspan = "2">&nbsp;</th>
  	<th>Shop KPI</th>
  	<th colspan = "2">Staff</th>
  	<th>Hr</th>
  	<th>Amount</th>
  	<th>KPI</th>
  	<th>Hr</th>
  	<th>Amount</th>
  	<th>KPI</th>
  	<th>Hr</th>
  	<th>Amount</th>
  	<th>KPI</th>
  	<th>Hr</th>
  	<th>Amount</th>
  	<th>KPI</th>
  	<th>Hr</th>
  	<th>Amount</th>
  	<th>KPI</th>
  	<th colspan = "2">&nbsp;</th>
  </tr>
  <?php foreach ($staffKPI[$v['shop']] as $stfName => $value){ ?>
  		<tr>
  			<td colspan = "2">&nbsp;</td>
  			<th><?php echo $shopKPI?></th>
  			<td colspan = "2"><?php echo $stfName;?></td>
  			<?php if(isset($value[$arrWeek[0]['date_end']])){
  						if($value[$arrWeek[0]['date_end']]['hour'] != 0){
  						$w1avgKPI += $value[$arrWeek[0]['date_end']]['KPI'];
  						$w1count ++;}?>
  			<td >
  					<?php echo $value[$arrWeek[0]['date_end']]['hour']?>				
  			</td>
  			<td>
  				 	<?php echo $value[$arrWeek[0]['date_end']]['amount']?>
  			</td>
  			<td><span
    				<?php
	    
						$color = "";
						if($value[$arrWeek[0]['date_end']]['KPI'] > $shopKPI){
							$color = "background-color:#0F0;";
						}else{
							$color = "background-color:#C30;";
						}		
						?>
    					style="font-size:10px;<?php echo $color;?>" ><?php echo $value[$arrWeek[0]['date_end']]['KPI'] ?></span>
  			</td>
  			<?php }else{?>
  			<td colspan = "3">&nbsp;</td>
  			<?php }?>
  			<?php if(isset($value[$arrWeek[1]['date_end']])){
  						if($value[$arrWeek[1]['date_end']]['hour'] != 0){
  						$w2avgKPI += $value[$arrWeek[1]['date_end']]['KPI'];
  						$w2count ++;}?>
  			<td>
  					<?php echo $value[$arrWeek[1]['date_end']]['hour']?>				
  			</td>
  			<td>
  				 	<?php echo $value[$arrWeek[1]['date_end']]['amount']?>
  			</td>
  			<td>
  				 	<span
    				<?php
	    
						$color = "";
						if($value[$arrWeek[1]['date_end']]['KPI'] > $shopKPI){
							$color = "background-color:#0F0;";
						}else{
							$color = "background-color:#C30;";
						}		
						?>
    					style="font-size:10px;<?php echo $color;?>" ><?php echo $value[$arrWeek[1]['date_end']]['KPI'] ?></span>
  			</td>
  			<?php }else{?>
  			<td colspan = "3">&nbsp;</td>
  			<?php }?>
  			<?php if(isset($value[$arrWeek[2]['date_end']])){
  					if($value[$arrWeek[2]['date_end']]['hour'] != 0){
  					$w3avgKPI += $value[$arrWeek[2]['date_end']]['KPI'];
  					$w3count ++;}
  					?>
  			<td>
  					<?php echo $value[$arrWeek[2]['date_end']]['hour']?>				
  			</td>
  			<td>
  				 	<?php echo $value[$arrWeek[2]['date_end']]['amount']?>
  			</td>
  			<td>
  				 	<span
    				<?php
	    
						$color = "";
						if($value[$arrWeek[2]['date_end']]['KPI'] > $shopKPI){
							$color = "background-color:#0F0;";
						}else{
							$color = "background-color:#C30;";
						}		
						?>
    					style="font-size:10px;<?php echo $color;?>" ><?php echo $value[$arrWeek[2]['date_end']]['KPI'] ?></span>
  			</td>
  			<?php }else{?>
  			<td colspan = "3">&nbsp;</td>
  			<?php }?>
  			<?php if(isset($value[$arrWeek[3]['date_end']])){
  					if($value[$arrWeek[3]['date_end']]['hour'] != 0){
  					$w4avgKPI += $value[$arrWeek[3]['date_end']]['KPI'];
  					$w4count ++;}?>
  			<td>
  					<?php echo $value[$arrWeek[3]['date_end']]['hour']?>				
  			</td>
  			<td>
  				 	<?php echo $value[$arrWeek[3]['date_end']]['amount']?>
  			</td>
  			<td>
  				 	<span
    				<?php
	    
						$color = "";
						if($value[$arrWeek[3]['date_end']]['KPI'] > $shopKPI){
							$color = "background-color:#0F0;";
						}else{
							$color = "background-color:#C30;";
						}		
						?>
    					style="font-size:10px;<?php echo $color;?>" ><?php echo $value[$arrWeek[3]['date_end']]['KPI'] ?></span>
  			</td>
  			<?php }else{?>
  			<td colspan = "3">&nbsp;</td>
  			<?php }?>
  			<?php if(isset($arrWeek[4])){?>
  			<?php if(isset($value[$arrWeek[4]['date_end']])){
  						if($value[$arrWeek[4]['date_end']]['hour'] != 0){
  						$w5avgKPI += $value[$arrWeek[4]['date_end']]['KPI'];
  						$w5count ++;}?>
  			<td>
  					<?php echo $value[$arrWeek[4]['date_end']]['hour']?>				
  			</td>
  			<td>
  				 	<?php echo $value[$arrWeek[4]['date_end']]['amount']?>
  			</td>
  			<td>
  				 	<span
    				<?php
	    
						$color = "";
						if($value[$arrWeek[4]['date_end']]['KPI'] > $shopKPI){
							$color = "background-color:#0F0;";
						}else{
							$color = "background-color:#C30;";
						}		
						?>
    					style="font-size:10px;<?php echo $color;?>" ><?php echo $value[$arrWeek[4]['date_end']]['KPI'] ?></span>
  			</td>
  			<?php }}else{?>
  			<td colspan = "3">&nbsp;</td>
  			<?php }?>
  			<td colspan = "2">&nbsp;</td>
  		</tr>
  <?php 	
  		} ?>
  		<tr>
  			<td colspan = "5">&nbsp;</td>
  			<th colspan = "2">W1 AVG KPI</th>
  			<td><?php if(isset($w1avgKPI) && $w1count != 0){
							echo round($w1avgKPI/$w1count,2);$avgKPI += round($w1avgKPI/$w1count,2);
  						}else{ ?>
  							&nbsp;
  				  <?php }?></td>
  			<th colspan = "2">W2 AVG KPI</th>	  
  			<td><?php if(isset($w2avgKPI) && $w2count != 0){
							echo round($w2avgKPI/$w2count,2);$avgKPI += round($w2avgKPI/$w2count,2);
  						}else{ ?>
  							&nbsp;
  				  <?php }?></td>
  			<th colspan = "2">W3 AVG KPI</th>
  			<td><?php if(isset($w3avgKPI) && $w3count != 0){
							echo round($w3avgKPI/$w3count,2);$avgKPI += round($w3avgKPI/$w3count,2);
  						}else{ ?>
  							&nbsp;
  				  <?php }?></td>
  			<th colspan = "2">W4 AVG KPI</th>
  			<td><?php if(isset($w4avgKPI) && $w4count != 0){
							echo round($w4avgKPI/$w4count,2);$avgKPI += round($w4avgKPI/$w4count,2);
  						}else{ ?>
  							&nbsp;
  				  <?php }?></td>
  			<th colspan = "2">W5 AVG KPI</th>
  			<td><?php if(isset($w5avgKPI) && $w5count != 0){
							echo round($w5avgKPI/$w5count,2);$avgKPI += round($w5avgKPI/$w5count,2);
  						}else{ ?>
  							&nbsp;
  				  <?php }?></td>
  			<th>Monthly<br/>AVG KPI</th>
  			<td><?php if(isset($arrWeek[4])){?>
  				<span
    				<?php
						$color = "";
						if(round($avgKPI/5,2) > $shopKPI){
							$color = "background-color:#0F0;";
						}else{
							$color = "background-color:#C30;";
						}		
						?>
    					style="font-size:10px;<?php echo $color;?>" >
    					
  					<?php echo round($avgKPI/5,2);?>
  					</span>
  			<?php }else{?>
  				<span
    				<?php
						$color = "";
						if(round($avgKPI/4,2) > $shopKPI){
							$color = "background-color:#0F0;";
						}else{
							$color = "background-color:#C30;";
						}		
						?>
    					style="font-size:10px;<?php echo $color;?>" >
  				<?php echo round($avgKPI/4,2);?></span>
  			<?php }?>
  			</td>
  			
  		</tr>
 <?php }?>
<?php } ?> 
</table>
<div style=" font-size:10px; color:#666;float:right;" >Developer:Leo,Date:2015-07-22,,ver:1.02</div>
</body>
</html>
<?php

$mailbody = ob_get_contents();
if($this->ifemail == "yes"){
$mail = new Model_Emailshandler();

$mail->sendNormalEmail($this->email,"Weekly Shop Target Summary Version 1.1", $mailbody);
if($this->email == "phonecollection.au@gmail.com"){
$mail->sendNormalEmail("admin@phonecollection.com.au;phonecollection.au@gmail.com","Weekly Shop Target Summary Version 1.1", $mailbody);	
	}



}
